import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 3489309323@qq.com
 * @create 2020-09-14-22:50
 */
public class P2线程串行化 {
  public static ExecutorService executor = Executors.newFixedThreadPool(10);

  public static void main(String[] args) throws ExecutionException, InterruptedException {
    /**thenRun/thenRunAsync*/
    /*System.out.println("main...start...");

    CompletableFuture<Void> voidCompletableFuture = CompletableFuture
      .supplyAsync(() -> {
        System.out.println("当前线程id: " + Thread.currentThread().getId());
        int i = 10 / 2;
        //int i = 10 / 0;
        System.out.println("运行结果: " + i);
        return i;
      }, executor)
      .handle((res,err)->{
        if (res != null) {
          return res * 2;
        }
        if (err != null) {
          return 0;
        }
        return 0;
      })
      .thenRunAsync(() -> {
        System.out.println("任务2启动了...");
      }*//*,executor*//*);

    System.out.println("main...end...");*/
/*
如果是.thenRunAsync:
main...start...
当前线程id: 20
运行结果: 5
main...end...
任务2启动了...

如果是.thenRun
main...start...
当前线程id: 20
运行结果: 5
任务2启动了...
main...end...
* */

    /** thenAccept/thenAcceptAsync*/
    /*System.out.println("main...start...");

    CompletableFuture<Void> voidCompletableFuture = CompletableFuture
      .supplyAsync(() -> {
        System.out.println("当前线程id: " + Thread.currentThread().getId());
        int i = 10 / 2;
        //int i = 10 / 0;
        System.out.println("运行结果: " + i);
        return i;
      }, executor)
      .handle((res,err)->{
        if (res != null) {
          return res * 2;
        }
        if (err != null) {
          return 0;
        }
        return 0;
      })
      .thenAcceptAsync((res) -> {
        System.out.println("res:"+res);
      });

    System.out.println("main...end...");*/
/*
如果是 thenAccept:
main...start...
当前线程id: 20
运行结果: 5
res:10
main...end...

如果是 thenAcceptAsync:
main...start...
当前线程id: 20
运行结果: 5
main...end...
res:10
* */

    /** thenApply/thenApplyAsync*/
    System.out.println("main...start...");

    CompletableFuture<String> future = CompletableFuture
      .supplyAsync(() -> {
        System.out.println("当前线程id: " + Thread.currentThread().getId());
        int i = 10 / 2;
        //int i = 10 / 0;
        System.out.println("运行结果: " + i);
        return i;
      }, executor)
      .handle((res, err) -> {
        if (res != null) {
          return res * 2;
        }
        if (err != null) {
          return 0;
        }
        return 0;
      })
      .thenApplyAsync((res) -> {
        System.out.println("任务二执行");
        System.out.println("接受到的res:" + res);
        return "Hello" + res;
      },executor);

    System.out.println("future.get():" + future.get());
    System.out.println("main...end...");
/*
main...start...
当前线程id: 20
运行结果: 5
任务二执行
接受到的res:10
future.get():Hello10
main...end...
* */
  }
}
